* Prepare IFS dataset

use "IFS/input/IFS.dta", clear
set more off
drop if attribute=="Status"

*Reshape data to have country-years as rows and variables as columns
gen id = _n
ren ïcountryname countryname
drop v108
destring v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48 v49 v50 v51 v52 v53 v54 v55 v56 v57 v58 v59 v60 v61 v62 v63 v64 v65 v66 v67 v68 v69 v70 v71 v72 v73 v74 v75 v76 v77 v78 v79 v80 v81 v82 v83 v84 v85 v86 v87 v88 v89 v90 v91 v92 v93 v94 v95 v96 v97 v98 v99 v100 v101 v102 v103 v104 v105, replace
reshape long v, i(id) j(year) // From wide to long, make each country-year a separate observation for each variable
drop id indicatorname baseyear
reshape wide v, i(countryname year) j(indicatorcode) string // From long to wide with each variable showing the value for each indicator

replace year = year+1914
ren vENDE_XDC_USD_RATE E1 // Exchange Rates, Domestic Currency per U.S. Dollar, End of Period, Rate
ren vENDA_XDC_USD_RATE E4 // Exchange Rates, Domestic Currency per U.S. Dollar, Period Average, Rate
ren vENEER_IX E5          // Exchange Rates, Nominal Effective Exchange Rate, Index.
ren vEREER_IX e1          // Exchange Rates, Real Effective Exchange Rate based on Consumer Price Index, Index.
ren vPCPI_IX CPI1         // Prices, Consumer Price Index, All Items, Index.
ren vPPPI_IX PPI1         // Prices, Producer Price Index, All Commodities, Index
ren vPWPI_IX WPI1         // Prices, Wholesale Price Index, All Items, Index
ren vNGDP_R_K_IX RGDP1    // Gross Domestic Product, Expenditure Approach, Real, Spliced Historical Series, Index
ren vNGDP_R_K_SA_IX RGDP2 // Gross Domestic Product, Expenditure Approach, Real, Spliced Historical Series, Seasonally Adjusted, Index
ren vNGDP_XDC GDP         // Gross Domestic Product, Expenditure Approach, Nominal, Domestic Currency

// Drops unnecessary countries
sort countryname year
drop if (countrycode==110 | countrycode==901 | countrycode==758 | countrycode==309 | countrycode==505 | countrycode==200 | countrycode==903 | countrycode==163 | countrycode==603 | countrycode==759 | countrycode==205 | countrycode==1)

// Add CPI data for Argentina
merge 1:1 countrycode year using "IFS/input/CPIArgentina.dta"
drop _merge

// Labels variable with names from IFS metadata
qui do "IFS/labels.do"

// Bilateral CPI-based RER with the US (e7)
sort countryname year
drop attribute
gen CPIdum = 0
replace CPIdum=1 if countryname=="United States"
gen CPIconstruct = CPI1*CPIdum
bys year: egen CPIus = total(CPIconstruct)
gen e7 =  1/ (E4 * CPIus / CPI1)
replace e7 = 1/(E4 * CPIus / CPI2) if countryname=="Argentina"
gen dum2 = 0
replace dum2 = 1 if year == 2010
gen e7bis = e7  * dum2
bys countryname: egen e72010 = total(e7bis)
replace e7 = e7 / e72010 * 100
gen E1inv = 1 / E1
sort countryname year
rename countrycode imfcode
sort imfcode
merge m:1 imfcode using "IFS/input/countrycodes.dta" // add 3 letter ISO codes
keep if _merge==3
drop _merge
rename isocode ccode
order ccode year countryname

// Bilateral PPI-based RER with the US (e8)
drop dum
gen dum = 0
replace dum =1 if countryname == "United States"
gen PPIconstruct = PPI1 * dum
bys year: egen PPIus = total(PPIconstruct)
gen e8 =  1/ (E4 * PPIus / PPI1)

// Bilateral WPI-based RER with the US (e9)
gen e9 =  1/ (E4 * PPIus / WPI1)

sort ccode year
save "IFS/output/IFS.dta", replace 
